package com.icexxx.braid;

/**
 * 字符串加密解密工具类
 * 
 * @author IceWater
 * @version 1.0.0
 */
public class IceBraid {
    /**
     * 对字符串进行加密
     * @param str 原始字符串(只支持英文、数字和特殊字符)
     * @return 加密后的字符串
     */
    public static String encode(String str) {
        int len = str.length();
        int group = len / 8;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < group; i++) {
            String item = str.substring(i * 8, i * 8 + 8);
            String itemNew = sexagesimal(item);
            sb.append(itemNew);
        }
        int tailLength = len - group * 8;
        if (tailLength != 0) {
            String tail = str.substring(group * 8);
            String itemNew = sexagesimal(tail);
            sb.append(itemNew);
        }
        return sb.toString();
    }

    private static String characterv(String str) {
        String sum = "";
        for (int i = 0; i < str.length(); i++) {
            String substring = str.substring(i, i + 1);
            int convv = convv(substring);
            if (convv < 10) {
                sum += "0" + convv;
            } else {
                sum += convv;
            }
        }
        return sum;
    }

    private static String sexagesimal(String str) {
        String conm = characterv(str);
        Long numLong = Long.parseLong(conm);
        long num1 = numLong / 167961600000000L;
        long surplus1 = numLong - num1 * 167961600000000L;
        long num2 = surplus1 / 2799360000000L;
        long surplus2 = surplus1 - num2 * 2799360000000L;
        long num3 = surplus2 / 46656000000L;
        long surplus3 = surplus2 - num3 * 46656000000L;
        long num4 = surplus3 / 777600000;
        long surplus4 = surplus3 - num4 * 777600000;
        long num5 = surplus4 / 12960000;
        long surplus5 = surplus4 - num5 * 12960000;
        long num6 = surplus5 / 216000;
        long surplus6 = surplus5 - num6 * 216000;
        long num7 = surplus6 / 3600;
        long surplus7 = surplus6 - num7 * 3600;
        long num8 = surplus7 / 60;
        long surplus8 = surplus7 - num8 * 60;
        long num9 = surplus8 / 1;
        String sum = "";
        int num11 = (int) num1;
        int num12 = (int) num2;
        int num13 = (int) num3;
        int num14 = (int) num4;
        int num15 = (int) num5;
        int num16 = (int) num6;
        int num17 = (int) num7;
        int num18 = (int) num8;
        int num19 = (int) num9;
        sum = honeycombv(num11) + honeycombv(num12) + honeycombv(num13) + honeycombv(num14) + honeycombv(num15) + honeycombv(num16) + honeycombv(num17)
                + honeycombv(num18) + honeycombv(num19);
        return sum;
    }

    private static int conv(String str) {
        int num = str.charAt(0);
        if (num == 0) {
            return 0;
        }
        if (num == 9) {
            return 97;
        }
        if (num == 10) {
            return 98;
        }
        if (num == 13) {
            return 99;
        }
        if (num >= 97 && num <= 126) {
            return num - 96;
        }
        return num;
    }

    private static int conw(int num) {
        if (num == 0) {
            return 0;
        }
        if (num == 97) {
            return 9;
        }
        if (num == 98) {
            return 10;
        }
        if (num == 99) {
            return 13;
        }
        if (num >= 1 && num <= 30) {
            return num + 96;
        }
        return num;
    }

    private static int convv(String str) {
        int[] array = { 0, 48, 85, 55, 59, 97, 70, 3, 35, 58, 54, 42, 20, 5, 50, 6, 66, 69, 75, 52, 26, 61, 10, 8, 81,
                16, 64, 41, 19, 90, 4, 49, 95, 92, 24, 45, 56, 60, 7, 77, 9, 87, 76, 67, 71, 13, 38, 78, 84, 40, 68, 53,
                33, 31, 83, 51, 57, 79, 39, 93, 30, 82, 63, 72, 23, 99, 12, 32, 22, 21, 14, 88, 80, 96, 18, 86, 29, 44,
                43, 94, 37, 1, 34, 91, 46, 65, 73, 2, 15, 98, 27, 74, 11, 36, 62, 25, 17, 28, 89, 47 };
        return array[conv(str)];
    }

    private static String honeycombv(int num) {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ0123456789";
        return str.substring(num, num + 1);
    }
    /**
     * 对字符串进行解密
     * @param str 原始字符串
     * @return 解密后的字符串
     */
    public static String decode(String str) {
        int len = str.length();
        int group = len / 9;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < group; i++) {
            String item = str.substring(i * 9, i * 9 + 9);
            String itemNew = decimalism(item);
            sb.append(itemNew);
        }
        return sb.toString();
    }

    private static int honeycombw(String str) {
        String alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ0123456789";
        return alphabet.indexOf(str);
    }

    private static String decimalism(String str) {
        long sum = 0;
        long[] array = new long[9];
        for (int i = 0; i < str.length(); i++) {
            String item = str.substring(i, i + 1);
            int cony = honeycombw(item);// 0-59
            array[i] = cony;
        }
        sum = sum + array[0] * 167961600000000L + array[1] * 2799360000000L + array[2] * 46656000000L
                + array[3] * 777600000 + array[4] * 12960000 + array[5] * 216000 + array[6] * 3600 + array[7] * 60
                + array[8];
        return characterw(sum);
    }

    private static String conww(int num) {
        int[] array = { 0, 81, 87, 7, 30, 13, 15, 38, 23, 40, 22, 92, 66, 45, 70, 88, 25, 96, 74, 28, 12, 69, 68, 64,
                34, 95, 20, 90, 97, 76, 60, 53, 67, 52, 82, 8, 93, 80, 46, 58, 49, 27, 11, 78, 77, 35, 84, 99, 1, 31,
                14, 55, 19, 51, 10, 3, 36, 56, 9, 4, 37, 21, 94, 62, 26, 85, 16, 43, 50, 17, 6, 44, 63, 86, 91, 18, 42,
                39, 47, 57, 72, 24, 61, 54, 48, 2, 75, 41, 71, 98, 29, 83, 33, 59, 79, 32, 73, 5, 89, 65 };
        int code = conw(array[num]);
        if (num == 0) {
            return "";
        }
        return String.valueOf((char) code);
    }

    private static String characterw(long num) {
        long num1 = num / 100000000000000L;
        long surplus1 = num - num1 * 100000000000000L;
        long num2 = surplus1 / 1000000000000L;
        long surplus2 = surplus1 - num2 * 1000000000000L;
        long num3 = surplus2 / 10000000000L;
        long surplus3 = surplus2 - num3 * 10000000000L;
        long num4 = surplus3 / 100000000L;
        long surplus4 = surplus3 - num4 * 100000000L;
        long num5 = surplus4 / 1000000L;
        long surplus5 = surplus4 - num5 * 1000000L;
        long num6 = surplus5 / 10000L;
        long surplus6 = surplus5 - num6 * 10000L;
        long num7 = surplus6 / 100L;
        long surplus7 = surplus6 - num7 * 100L;
        long num8 = surplus7;
        int numInt1 = (int) num1;
        int numInt2 = (int) num2;
        int numInt3 = (int) num3;
        int numInt4 = (int) num4;
        int numInt5 = (int) num5;
        int numInt6 = (int) num6;
        int numInt7 = (int) num7;
        int numInt8 = (int) num8;
        String contt1 = conww(numInt1);
        String contt2 = conww(numInt2);
        String contt3 = conww(numInt3);
        String contt4 = conww(numInt4);
        String contt5 = conww(numInt5);
        String contt6 = conww(numInt6);
        String contt7 = conww(numInt7);
        String contt8 = conww(numInt8);
        return contt1 + contt2 + contt3 + contt4 + contt5 + contt6 + contt7 + contt8;
    }
}
